/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package unipiloto.edu.co.generador_reportes.service.admin;

import unipiloto.edu.co.generador_reportes.service.tracker.ServiceLocator;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.osgi.framework.BundleContext;

/**
 *
 * @author David
 */
@WebServlet(name = "GenerateReportServlet", urlPatterns = {"/generateReport"})
public class GenerateReportServlet extends HttpServlet {

    private static final Logger logger = Logger.getLogger("unipiloto.edu.co");

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        ServiceLocator locator = new ServiceLocator(getBundleContext());
        String reportName = request.getParameter("reportName");
        int exportFormat = Integer.parseInt(request.getParameter("reportExportFormat"));
        Map<String, Object> parametros = new HashMap<String, Object>();

        try {
            switch (exportFormat) {
                case 1:
                    locator.getReportGeneratorService(-1).generateReportToRTF(response, reportName, parametros);
                    break;
                case 2:
                    locator.getReportGeneratorService(-1).generateReportToPDF(response, reportName, parametros);
                    break;
                case 3:
                    locator.getReportGeneratorService(-1).generateReportToCSV(response, reportName, parametros);
                    break;
                case 4:
                    locator.getReportGeneratorService(-1).generateReportToXML(response, reportName, parametros);
                    break;
                case 5:
                    locator.getReportGeneratorService(-1).generateReportToHTML(response, reportName, parametros);
                    break;
                case 6:
                    locator.getReportGeneratorService(-1).generateReportToXLS(response, reportName, parametros);
                    break;
                case 7:
                    locator.getReportGeneratorService(-1).generateReportToJXL(response, reportName, parametros);
                    break;
                case 8:
                    locator.getReportGeneratorService(-1).generateReportToODT(response, reportName, parametros);
                    break;
                case 9:
                    locator.getReportGeneratorService(-1).generateReportToODS(response, reportName, parametros);
                    break;
                case 10:
                    locator.getReportGeneratorService(-1).generateReportToDOCX(response, reportName, parametros);
                    break;
                case 11:
                    locator.getReportGeneratorService(-1).generateReportToXLSX(response, reportName, parametros);
                    break;
                case 12:
                    locator.getReportGeneratorService(-1).generateReportToPPTX(response, reportName, parametros);
                    break;
                case 13:
                    locator.getReportGeneratorService(-1).generateReportToXHTML(response, reportName, parametros);
                    break;
                default:
                    break;
            }
        } catch (ServiceLocator.ServiceUnavailableException e) {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            logger.log(Level.SEVERE, "El servicio no se encuentra disponible... -> {0} <-", reportName);
            out.println("El servicio no se encuentra disponible...");
        }
    }

    private BundleContext getBundleContext() {
        return BundleContext.class.cast(getServletConfig().getServletContext().getAttribute("osgi-bundlecontext"));
    }
}
